package com.yyw.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.Service.ShareService;
import com.yyw.bean.Share;
import com.yyw.bean.User;
import com.yyw.dao.ShareDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "分享文API")
@RestController
@RequestMapping("/user")
@Slf4j
public class ShareController {

    @Value("${my.session.user}")
    private String userAttr;

    @Autowired
    private ShareService shareService;

    @Autowired
    private ShareDao shareDao;

    @CrossOrigin
    @ApiOperation(value = "赞同分享文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "分享文id", required = true) })
    @GetMapping("/agreeShare")
    public Result agreeShare(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        User authorByShareId = shareDao.getAuthorByShareId(id);
        Share share = shareDao.getShareById(id);
        if (share != null) {
            Integer agreenum = share.getAgreenum();
            if (agreenum == null) {
                agreenum = 0;
            }
            shareDao.updateShareAgreeNum(id, agreenum + 1);
        }
        return ResultFactory.success("赞同成功");
    }

    @CrossOrigin
    @ApiOperation(value = "根据分享文id获取作者信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "分享文id", required = true) })
    @GetMapping("/getAuthorByShareId")
    public Result getAuthorByShareId(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        User authorByShareId = shareDao.getAuthorByShareId(id);
        return ResultFactory.success(authorByShareId);
    }

    @CrossOrigin
    @ApiOperation(value = "获得指定分享文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "分享文id", required = true),
            @ApiImplicitParam(name = "isCollect", value = "是否获取收藏状态，默认false", required = true) })
    @GetMapping("/getShare")
    public Result getShare(@RequestParam("id") Long id,
            @RequestParam(value = "isCollect", defaultValue = "false", required = false) Boolean isCollect) {
        Long userid = null;
        if (isCollect) {
            userid = MyContext.getSessionUser().getId();
        }
        return shareService.getShare(id, userid);
    }

    @CrossOrigin
    @ApiOperation(value = "删除分享文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "分享文id", required = true) })
    @GetMapping("/deleteShare")
    public Result deleteShare(@RequestParam("id") Long id) {
        return shareService.deleteShare(id);
    }

    @CrossOrigin
    @ApiOperation(value = "添加新分享文", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "title", value = "标题", required = true),
            @ApiImplicitParam(name = "content", value = "内容", required = true),
            @ApiImplicitParam(name = "level", value = "难度", required = true) })
    @PostMapping("/saveShare")
    public Result saveShare(@RequestParam("title") String title, @RequestParam("level") Integer level,
            @RequestParam("content") String content, @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute(userAttr);
        Share share = new Share(null, title, level, content, user.getId(), null, null, null, false);

        return shareService.saveShare(share);
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取分享文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false),
            @ApiImplicitParam(name = "t", value = "排序规则,1 发布时间升序，2发布时间降序，3赞同数降序", required = false),
            @ApiImplicitParam(name = "search", value = "搜索内容", required = false) })
    @GetMapping("/getShareList")
    public Result getShareList(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum,
            @RequestParam(value = "t", defaultValue = "1", required = false) Integer t,
            @RequestParam(value = "search", required = false) String search) {
        User user = MyContext.getSessionUser();
        return shareService.getShares(user.getLevel(), pageNo, pageNum, t, search);
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取用户自己的分享文", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getUserShareList")
    public Result getUserShareList(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        User user = MyContext.getSessionUser();
        return shareService.getUserShares(pageNo, pageNum, user.getId());
    }

}
